<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of welcome
 *
 * @author Obi
 */
class Login_signup extends MY_Controller {

    function __construct() {
        parent::__construct();
        $this->m_member = new M_member();
        $this->m_kota = new M_kota();
        $this->m_provinsi = new M_provinsi();
    }

    function index($error = NULL, $log_sukses = NULL) {

        $data['title'] = 'Data Produk';


        $data['menu'] = array(
        );

        $data['title'] = "Otorisasi Pengguna";
        $data['log_sukses'] = $log_sukses;
        $data['error'] = $error;
        $data['list_provinsi'] = $this->m_provinsi->get();

        $data['isi'] = 'isi_login_signup';
        $this->load->view('template_client', $data);
    }

    function simpan() {
        $data = array(
            'email' => $this->input->post('email'),
            'password' => md5($this->input->post('password')),
            'nama_depan' => $this->input->post('nama_depan'),
            'nama_belakang' => $this->input->post('nama_belakang'),
            'id_provinsi' => $this->input->post('provinsi'),
            'id_kota' => $this->input->post('kota'),
            'alamat' => $this->input->post('alamat'),
            'kode_pos' => $this->input->post('kode_pos'),
            'telp' => $this->input->post('telp'),
            'status' => 1,
        );

        $email = $this->input->post('email');
        $cek = $this->m_member->where("email = '$email'")->count();

        if ($cek < 1) {
            $this->m_member->insert($data);
            $this->index("", 1);
        } else {
            $this->index("", 666);
        }
    }

    function otentikasi() {

        $email = mysql_real_escape_string($this->input->post('email'));
        $password = md5($this->input->post('password'));

        $jumlah_dt = $this->m_member->where("email = '$email' and password = '$password'")->count();
        if ($jumlah_dt == 1) {
            $gt_dt = $this->m_member->where("email = '$email' and password = '$password'")->get();

            if ($gt_dt->password == $password) {
                $data_log = array(
                    'id_member' => $gt_dt->id,
                    'email' => $gt_dt->email,
                    'nama_depan' => $gt_dt->nama_depan,
                    'masuk' => TRUE
                );
                $this->session->set_userdata($data_log);

                redirect('main/produk');
            } else {
                $data['error'] = "User Login dan Password Salah, silahkan dicoba lagi";
                echo $masuk = $this->session->userdata('masuk');
                $this->index("Email dan Password tidak sesuai");
            }
        } else {
            $data['error'] = "User Login dan Password Salah, silahkan dicoba lagi";
            echo $masuk = $this->session->userdata('masuk');
            $this->index("Email dan Password tidak sesuai");
        }
    }

    function logout() {
        $data_log = array(
            'id_member' => "",
            'email' => "",
            'nama_depan' => "",
            'masuk' => false
        );
        $this->session->unset_userdata($data_log);
        $this->index();
    }

}

?>
